package cn.openea.perpartner.cms.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.cms.web.mapper.CmsUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.openea.perpartner.cms.web.entity.CmsUser;
import cn.openea.perpartner.cms.web.service.ICmsUserService;
import cn.openea.perpartner.common.auth.entity.LoginBody;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (CmsUser)表服务实现类
 *
 * @author makejava
 * @since 2022-07-28 14:10:32
 */
@Service("cmsUserService")
public class CmsUserServiceImpl extends ServiceImpl<CmsUserMapper, CmsUser> implements ICmsUserService {

    @Override
    public void save(LoginBody loginUser) {
        LambdaQueryWrapper<CmsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsUser::getUserId, loginUser.getUserid());
        CmsUser user = baseMapper.selectOne(wrapper);
        if (BeanUtil.isEmpty(user)) {
            user = new CmsUser();
            user.setUserId(loginUser.getUserid());
            user.setNickName(loginUser.getUsername());
            baseMapper.insert(user);
        }
    }

    @Override
    public List<CmsUser> selectUserList(String searchStr) {
        //1.先从客户表查询所有销售用户
        LambdaQueryWrapper<CmsUser> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(searchStr)) {
            wrapper.and(wrap -> wrap.or().like(CmsUser::getNickName, searchStr)
                    .or().like(CmsUser::getUserId, searchStr));
        }
        wrapper.orderByAsc(CmsUser::getNickName);
        return baseMapper.selectList(wrapper);
    }
}

